///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZRULE_H_
#define _BUOLA_ZZ_ZRULE_H_

#include <buola/zz/zbase.h>

namespace buola { namespace zz {

template<typename tIt,typename tAttr=UUnused,typename tSk=UUnused>
class ZRule : public ZBase<ZRule<tIt,tAttr,tSk>>
{
    struct SContext
    {
        tAttr mAttribute;
    };
    
    struct SHolderBase
    {
        virtual ~SHolderBase() {}
        
        virtual bool Parse(tIt &pB,const tIt &pE,SContext &pCtx,const tSk &pSk) const=0;
    };
    
    template<typename tExpr>
    struct SHolder : public SHolderBase
    {
        template<typename tPExpr>
        SHolder(tPExpr &&pExpr)
            :   mExpr(std::forward<tPExpr>(pExpr))
        {}
        
        bool Parse(tIt &pB,const tIt &pE,SContext &pCtx,const tSk &pSk) const
        {
            tAttr lAttr;
            UUnused lCtx;
            if(!mExpr.Parse(pB,pE,lAttr,lCtx,pSk))
                return false;
            pCtx.mAttribute=lAttr;
            return true;
        }
        
        tExpr mExpr;
    };
    
    template<typename tExpr>
    struct SIsolatingHolder : public SHolderBase
    {
        template<typename tPExpr>
        SIsolatingHolder(tPExpr &&pExpr)
            :   mExpr(std::forward<tPExpr>(pExpr))
        {}
        
        bool Parse(tIt &pB,const tIt &pE,SContext &pCtx,const tSk &pSk) const
        {
            UUnused lDummy;
            return mExpr.Parse(pB,pE,lDummy,pCtx,pSk);
        }
        
        tExpr mExpr;
    };
    
    struct SHolderPtr : public UShared
    {
        std::unique_ptr<SHolderBase> mHolder;
    };
    
public:
    template<typename tDummy>
    struct tAttribute
    {
        typedef tAttr tType;
    };
    
    ZRule()
        :   mHolderPtr(new SHolderPtr)
    {}
    
    ZRule(const ZRule &pRH)
        :   mHolderPtr(pRH.mHolderPtr)
    {}
    
    ~ZRule()
    {}
    
    template<typename tExpr>
    explicit ZRule(const tExpr &pExpr)
    {
        operator=(pExpr);
    }
    
    template<typename tUnusedCtx,typename tPAttr>
    bool Parse(tIt &pB,const tIt &pE,tPAttr &pAttr,tUnusedCtx&,const tSk &pSk) const
    {
        if(!mHolderPtr->mHolder)
            return false;
        
        SContext lCtx;
        
        if(!mHolderPtr->mHolder->Parse(pB,pE,lCtx,pSk)) return false;
        pAttr=lCtx.mAttribute;
        return true;
    }
    
    template<typename tExpr>
    ZRule &operator%=(tExpr &&pExpr)
    {
        mHolderPtr->mHolder.reset(new SHolder<ODecay<tExpr>>(std::forward<tExpr>(pExpr)));
        return *this;
    }
    
    template<typename tExpr>
    ZRule &operator=(tExpr &&pExpr)
    {
        mHolderPtr->mHolder.reset(new SIsolatingHolder<ODecay<tExpr>>(std::forward<tExpr>(pExpr)));
        return *this;
    }
    
private:
    intrusive_ptr<SHolderPtr> mHolderPtr;
};

/*namespace zz*/ } /*namespace buola*/ }

#endif
